package com.hznu.ucenter.controller;


import com.hznu.commonutils.R;
import com.hznu.ucenter.entity.Center;
import com.hznu.ucenter.entity.Decrypt;
import com.hznu.ucenter.entity.Vehicle;
import com.hznu.ucenter.entity.vo.SendMsgVo;
import com.hznu.ucenter.func.cpabe.object.InterValue;
import com.hznu.ucenter.func.main.Cpabe;
import com.hznu.ucenter.service.CenterService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cdd
 * @since 2021-03-19
 */
@RestController
@RequestMapping("/center")
@Api(tags = "云存储中心")
public class CenterController {

    @Autowired
    private CenterService centerService;

    /**
     * 信息中心显示信息列表
     * @return
     */
    @GetMapping("getListCenter")
    public R getListCenter() {
        List<Center> list = centerService.list(null);
        return R.ok().data("list",list);
    }

    @PostMapping("addCenter")
    public R addCenter(@RequestBody Center center) {
        centerService.insertCenter(center);
        return R.ok();
    }

    @GetMapping("deleteCenter/{centerId}")
    public R deleteCenter(@PathVariable Long centerId) {
        centerService.removeCenter(centerId);
        return R.ok();
    }

    @PostMapping("updateCenter")
    public R updateCenter(@RequestBody Center center) {
        centerService.updateCenter(center);
        return R.ok();
    }


    // 发送信息至RSU
    @PostMapping("sendMsg")
    public R sendMsg(@RequestBody SendMsgVo sendMsgVo) {
//        System.out.println(sendMsgVo.getCenterId());
//        System.out.println(sendMsgVo.getMsgStrategy());
//        System.out.println(sendMsgVo.getRsuId());
        centerService.encodeAndSend(sendMsgVo);
        return R.ok();
    }

    //=================Android交互======================
    // 车辆获得私钥
    @PostMapping("keygen")  // android 发送车辆信息
    public R getPriKey(@RequestBody Vehicle vehicle){
        centerService.getPriKey(vehicle);
        return R.ok();
    }

    // 解密
    @PostMapping("decode")  //
    public R decode(@RequestBody Decrypt decrypt){
        List<String> decode = centerService.decode(decrypt);
        return R.ok();
    }

}

